﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Definition;
using Besture.Model;
using Besture.Utils;

namespace Besture.UI
{
    public partial class JobChanceSet : BasePage
    {


        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            if (!IsPostBack)
            {
                bindProvice();
                if (Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    //编辑编号
                    hidEditID.Value = Request.QueryString["id"];
                    if (!Fun.IsNumber(hidEditID.Value))
                    {
                        RedirectErrorUrl("编号错误");
                    }
                    InitData(Convert.ToInt32(hidEditID.Value));
                }
            }
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            JobChanceDac dac = new JobChanceDac();
            JobChanceModel model = dac.GetModel(id);
            txtPositionTitle.Text = model.PositionTitle != null ? model.PositionTitle.ToString() : "";
            if (model.ProID>0)
            {
                ddlProvice.SelectedValue = model.ProID + "";
                bindCityId(model.ProID);
                ddlCityId.SelectedValue = model.CityId.ToString();
            }
            txtSort.Text = model.Sort != null ? model.Sort.ToString() : "";
        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            JobChanceDac dac = new JobChanceDac();
            JobChanceModel model = null;
            string editID = hidEditID.Value;
            //添加
            if (string.IsNullOrEmpty(editID))
            {
                model = new JobChanceModel();
                model.CityId = ddlCityId.SelectedValue != "" ? Convert.ToInt32(ddlCityId.SelectedValue) : 0;
                model.PositionTitle = txtPositionTitle.Text.Trim();
                if (!string.IsNullOrEmpty(txtSort.Text.Trim()))
                {
                    model.Sort = Convert.ToInt32(txtSort.Text.Trim());
                }
                model.IsDeleted = Constants.DELETE_FLG_0;
                model.CreateBy = LoginUserInfo.ID.ToString();
                model.CreateTime = DateTime.Now;
                model.UpdateBy = LoginUserInfo.ID.ToString();
                model.UpdateTime = DateTime.Now;
                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(Convert.ToInt32(editID));
                model.CityId = ddlCityId.SelectedValue != "" ? Convert.ToInt32(ddlCityId.SelectedValue) : 0;
                model.PositionTitle = txtPositionTitle.Text.Trim();
                if (!string.IsNullOrEmpty(txtSort.Text.Trim()))
                {
                    model.Sort = Convert.ToInt32(txtSort.Text.Trim());
                }
                model.UpdateBy = LoginUserInfo.ID.ToString();
                model.UpdateTime = DateTime.Now;
                dac.Update(model);
            }
            Fun.Alert("保存成功", "JobChanceList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("JobChanceList.aspx");
        }

        #endregion

        protected void ddlProvice_OnChanged(object sender, EventArgs eventArgs)
        {
            ddlCityId.Items.Clear();
            this.ddlCityId.Items.Add(new ListItem("请选择", ""));
            int proId = string.IsNullOrEmpty(ddlProvice.SelectedValue) ? 0 : Convert.ToInt32(ddlProvice.SelectedValue);
            bindCityId(proId);
        }
        /// <summary>
        ///绑定城市
        /// </summary>
        public void bindCityId(int proId)
        {
            CityDac dac = new CityDac();
            DataTable dt = dac.GetDataTable(proId);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlCityId.Items.Add(new ListItem(dt.Rows[i]["CityName"].ToString(), dt.Rows[i]["CityID"].ToString()));
            }
        }

        /// <summary>
        ///绑定城市
        /// </summary>
        public void bindProvice()
        {
            ProvinceDac dac = new ProvinceDac();
            DataTable dt = dac.GetDataTable();
            this.ddlProvice.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlProvice.Items.Add(new ListItem(dt.Rows[i]["ProName"].ToString(), dt.Rows[i]["ProID"].ToString()));
            }
        }
    }
}
